#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <Windows.h>

#include <memory>

#include "TimeMgr.h"

std::mutex some_mutex;


void hello() // 2
{
    Sleep(2000);
    std::cout << "Hello Concurrent World\n" << std::endl;
    std::cout<<std::this_thread::get_id() << std::endl;
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
//    , logthread(1)
{
    ui->setupUi(this);
    std::cout << "main thread id " << std::this_thread::get_id() << std::endl;

    TimeMgr timeMgr(TimeMgr::now());
    timeMgr.toString();

    plogthread = new Thread_test();

    background_task f;
    std::thread t(std::ref(f));
    t.join();

    background_task f2;
    std::thread t2(&background_task::noParam, &f2);
    t2.join();

    background_task f3;
    std::thread t3(&background_task::backWork, &f3, 1314);
    t3.join();
}

MainWindow::~MainWindow()
{
    delete plogthread;
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    td1 = std::thread(&background_task::lookTest, &m_backwork);
    td1.join();
}

void MainWindow::on_pushButton_2_clicked()
{
    td2 = std::thread(&background_task::lookTest, &m_backwork);
    td2.join();
}

//唤醒1
void MainWindow::on_pushButton_3_clicked()
{
//    td1 = std::thread(&background_task::wakeup, &m_backwork);
    m_backwork.wakeup();
}

void MainWindow::on_pushButton_4_clicked()
{
    td2 = std::thread(&background_task::testCond, &m_backwork);
}


//唤醒2
void MainWindow::on_pushButton_5_clicked()
{
    m_backwork.wakeup2();
}

void MainWindow::on_pushButton_6_clicked()
{
    int logCount = ui->spinBox->value();

    static int count = 1;
    char log[100];
    for (int n = 0; n< logCount; ++n)
    {
        sprintf(log, "asasasasasasas---%d\n", count++);
        plogthread->append(log);
    }
}
